package com.tsystems.train.dao;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import com.tsystems.train.entity.Station;


@Stateless
public class StationDAO extends AbstractDAO<Station> {

	public Station getStationByName(String name) {
		
		EntityManager em = getEntityManager();	
		CriteriaBuilder builder = em.getCriteriaBuilder();
		CriteriaQuery<Station> criteria = builder.createQuery(Station.class);
		
		Root<Station> stations = criteria.from(Station.class);
		criteria.select(stations);
		criteria.where(builder.equal(stations.get("name"), name));
		List<Station> result = em.createQuery(criteria).getResultList();
		
		return !result.isEmpty() ? result.get(0) : null;
	}
	
	public List<Station> getStationList() {
		
		EntityManager em = getEntityManager();	
		CriteriaBuilder builder = em.getCriteriaBuilder();
		CriteriaQuery<Station> criteria = builder.createQuery(Station.class);
		
		Root<Station> stations = criteria.from(Station.class);
		criteria.select(stations);
		
		List<Station> result = em.createQuery(criteria).getResultList();
		
		return  result;
	}
	
	
	
}
